﻿using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Sinacor.Infra.Tools;
using System.Collections.Generic;

namespace ActionRegistrationWeb
{
    public partial class Default : System.Web.UI.Page
    {
        private ActionDataGrid dataGrid = new ActionDataGrid("datagridAction", new string[] { "&nbsp;", "Action Id", "Action", "Método", "Método Alternativo", "Kernel Mais" });

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (!IsPostBack)
            {
                this.LoadDataGrid();
                btDeleteSelected.Attributes.Add("onclick", "return confirm('Você tem que certeza que deseja deletar o(s) item(s) selecionado(s)?');");
                //chkIsKernelPlus.Attributes.Add("onchange", "MudarValidacao();");
            }
        }

        private void LoadDataGrid()
        {
            this.grdForm.Controls.Clear();
            this.grdForm.Controls.Add(dataGrid);

            Action actionFilter = new Action();
            List<Action> list = new DataAccess().SelectAction(actionFilter);

            foreach (Action action in list)
            {
                HtmlTableRow row = new HtmlTableRow();
                row.Attributes.Add("onmouseover", "GridOnMouseOver(this)");
                row.Attributes.Add("onmouseout", "GridOnMouseOut(this)");
                row.Attributes.Add("onDblclick", "GridMouseDblClick('datagridAction', this)");
                row.Attributes.Add("onclick", "GridMouseClick('datagridAction', '" + action.ActionId.ToString() + "', true)");
                row.Attributes.Add("style", "cursor: hand");
                row.BgColor = "#ffffff";

                HtmlTableCell cellCheckBox = new HtmlTableCell();
                HtmlTableCell cellActionId = new HtmlTableCell();
                HtmlTableCell cellActionName = new HtmlTableCell();
                HtmlTableCell cellMethodName = new HtmlTableCell();
                HtmlTableCell cellMethodNameKernel = new HtmlTableCell();
                HtmlTableCell cellIsKernelPlus = new HtmlTableCell();

                cellCheckBox.Align = "center";
                cellActionId.Align = "center";

                CheckBox chk = new CheckBox();
                chk.ID = action.ActionId.ToString();
                chk.Enabled = false;

                cellCheckBox.Controls.Add(chk);
                cellActionId.InnerHtml = action.ActionId.ToString();
                cellActionName.InnerHtml = action.ActionName;
                cellMethodName.InnerHtml = action.MethodName;
                cellMethodNameKernel.InnerHtml = action.MethodNameKernel;
                cellIsKernelPlus.InnerHtml = action.IsKernelPlus ? "Sim" : "Não";

                row.Cells.Add(cellCheckBox);
                row.Cells.Add(cellActionId);
                row.Cells.Add(cellActionName);
                row.Cells.Add(cellMethodName);
                row.Cells.Add(cellMethodNameKernel);
                row.Cells.Add(cellIsKernelPlus);

                dataGrid.Rows.Add(row);
            }
        }

        //protected void chkIsKernelPlus_CheckedChanged(object sender, System.EventArgs e)
        //{
        //    rfvtxtMethodNameKernel.Enabled = chkIsKernelPlus.Checked;
        //    this.LoadDataGrid();
        //}

        protected void btSave_Click(object sender, System.EventArgs e)
        {
            DataAccess dataAccess = new DataAccess();
            bool IsUpdate = (!string.IsNullOrEmpty(hdnActionId.Value));

            try
            {
                Action action = new Action();
                action.ActionName = txtAction.Text;
                action.MethodName = txtMethodName.Text;
                action.MethodNameKernel = txtMethodNameKernel.Text;
                action.IsKernelPlus = chkIsKernelPlus.Checked;

                if (IsUpdate)
                {
                    action.ActionId = System.Convert.ToInt32(hdnActionId.Value);
                    dataAccess.UpdateAction(action);
                    lblMessage.Text = "<b><font color=#009900>Item salvo com sucesso!</font></b>";                    
                }
                else
                {
                    dataAccess.InsertAction(action);
                    lblMessage.Text = "<b><font color=#009900>Item salvo com sucesso!</font></b>";
                }

                LimparCampos();
                this.LoadDataGrid();
            }
            catch (System.Exception ex)
            {
                lblMessage.Text = "<b><font color=#990000>" + ex.Message + "</font></b>";
            }
        }

        protected void btDeleteSelected_Click(object sender, System.EventArgs e)
        {
            DataAccess dataAccess = new DataAccess();

            try
            {
                if (!string.IsNullOrEmpty(hdnSelected.Value))
                {
                    string[] comma = hdnSelected.Value.Split(',');

                    List<Action> actions = new List<Action>();

                    foreach (string s in comma)
                    {
                        actions.Add(new Action() { ActionId = System.Convert.ToInt32(s) });
                    }

                    if (actions.Count > 0)
                    {
                        int linhasAfetadas = dataAccess.DeleteAction(actions);
                        if (linhasAfetadas > 0)
                        {
                            if (linhasAfetadas == 1)
                                lblMessage.Text = "<b><font color=#009900>" + linhasAfetadas.ToString() + " item deletado com sucesso!</font></b>";
                            else
                                lblMessage.Text = "<b><font color=#009900>" + linhasAfetadas.ToString() + " itens deletados com sucesso!</font></b>";
                        }
                        else
                            lblMessage.Text = "<b><font color=#990000>Nenhuma linha deletada</font></b>";

                        this.LoadDataGrid();
                    }
                    else
                        lblMessage.Text = "<b><font color=#990000>Nenhum item selecionado para exclusão!</font></b>";
                }
                else
                    lblMessage.Text = "<b><font color=#990000>Nenhum item selecionado para exclusão!</font></b>";
            }
            catch (System.Exception ex)
            {
                lblMessage.Text = "<b><font color=#990000>" + ex.Message + "</font></b>";
            }
        }

        private void LimparCampos()
        {
            txtAction.Text = string.Empty;
            txtMethodName.Text = string.Empty;
            txtMethodNameKernel.Text = string.Empty;
            chkIsKernelPlus.Checked = false;
            hdnActionId.Value = "";
        }

        protected void ScriptManagerService_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
        {
            ScriptManagerService.AsyncPostBackErrorMessage = e.Exception.Message;
        }
    }
}